package com.dingshi.security;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.RepairApi;
import com.dingshi.security.entity.Detail;
import com.dingshi.security.util.Utils;
import com.dingshi.security.view.LoadDialog;

/**
 * 查看报修单
 * @author start
 *
 */
public class RepairHistoryActivity extends BaseActivity implements OnClickListener{
	protected static final int ID_LOADING = 2600;
	private Button btn_back;
	private TextView title_text;
	private Button btn_right;
	private TextView edit_deparment;
	private TextView edit_shangbao_date;
	private TextView edit_duty_man;
	private TextView edit_device;
	private TextView edit_break;
	private TextView edit_unit;
	private TextView edit_jiebao_date;
	private TextView edit_confirm_break;
	private TextView edit_suggestion;
	private TextView edit_repair_man;
	private TextView edit_predict_date;
	private TextView edit_repair_result;
	private TextView edit_finish;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repair_track_detail);
		
		
		initTitle();
		initComponents();
		registerButton();
		String oddNum = getIntent().getStringExtra("oddNum");
		getRepairDetail(oddNum,"1");
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ID_LOADING:
			LoadDialog dialog = new LoadDialog(this, getResources().getString(R.string.login_loading));
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}
	
	private void getRepairDetail(String oddNum,String contentType) {
		RepairApi api = new RepairApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				JSONObject jsonEntity = apiResult.getJsonEntity();
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					// {"d":[{"__type":"DiaryReturn:r","c":"aaa","t":"01:59:05"}],"t":1}

					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							JSONObject obj = jsonEntity.getJSONObject("d");
							Detail detail = null;
							if(obj!=null) {
								detail = new Detail();
								if(obj.has("i")) {
									detail.setI(obj.getString("i"));
								}
								if(obj.has("st")) {
									detail.setSt(obj.getString("st"));
								}
								if(obj.has("d")) {
									detail.setD(obj.getString("d"));
								}
								if(obj.has("xq")) {
									detail.setXq(obj.getString("xq"));
								}
								if(obj.has("s")) {
									detail.setS(obj.getString("s"));
								}
								if(obj.has("stel")) {
									detail.setStel(obj.getString("stel"));
								}
								if(obj.has("dt")) {
									detail.setDt(obj.getString("dt"));
								}
								if(obj.has("e")) {
									detail.setE(obj.getString("e"));
								}
								if(obj.has("rd")) {
									detail.setRd(obj.getString("rd"));
								}
								if(obj.has("p1")) {
									detail.setP1(obj.getString("p1"));
								}
								if(obj.has("p2")) {
									detail.setP2(obj.getString("p2"));
								}
								if(obj.has("p3")) {
									detail.setP3(obj.getString("p3"));
								}
								if(obj.has("p4")) {
									detail.setP4(obj.getString("p4"));
								}
								
								//维修信息
								if(obj.has("red")) {
									detail.setRed(obj.getString("red"));
								}
								if(obj.has("rdt")) {
									detail.setRdt(obj.getString("rdt"));
								}
								if(obj.has("rer")) {
									detail.setRer(obj.getString("rer"));
								}
								if(obj.has("rea")) {
									detail.setRea(obj.getString("rea"));
								}
								if(obj.has("w")) {
									detail.setW(obj.getString("w"));
								}
								if(obj.has("edt")) {
									detail.setEdt(obj.getString("edt"));
								}
								if(obj.has("res")) {
									detail.setRes(obj.getString("res"));
								}
								if(obj.has("rcd")) {
									detail.setRcd(obj.getString("rcd"));
								}
							}
							updateUI(detail);
							break;
						case 1:// 数组
							break;
						case -1:// 错误
							dismissDialog(ID_LOADING);
							break;

						default:
							dismissDialog(ID_LOADING);
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
		});
		api.getRepairDetail(1, oddNum,contentType);
	}
	
	private void updateUI(Detail detail) {
		if(detail!=null) {
			edit_deparment.setText(detail.getD());
			edit_shangbao_date.setText(detail.getDt());
			edit_duty_man.setText(detail.getS());
			edit_device.setText(detail.getE());
			edit_break.setText(detail.getRd());
			//维修信息
			edit_unit.setText(detail.getRed());
			edit_jiebao_date.setText(detail.getRdt());
			edit_confirm_break.setText(detail.getRer());
			edit_suggestion.setText(detail.getRea());
			edit_repair_man.setText(detail.getW());
			edit_predict_date.setText(detail.getEdt());
			edit_repair_result.setText(detail.getRes());
			edit_finish.setText(detail.getRcd());
		}
	}
	private void initTitle() {
		btn_back = (Button)findViewById(R.id.btn_back);
		title_text = (TextView)findViewById(R.id.title_text);
		btn_right = (Button)findViewById(R.id.btn_refresh);
		title_text.setText("查看报修单");
		
	}

	private void registerButton() {
		btn_right.setOnClickListener(this);
		btn_back.setOnClickListener(this);
	}

	private void initComponents() {
		LinearLayout item = (LinearLayout)findViewById(R.id.linearLayout1);
		item.setVisibility(View.VISIBLE);
		
		
		edit_deparment = (TextView)findViewById(R.id.edit_deparment);
		edit_shangbao_date = (TextView)findViewById(R.id.edit_shangbao_date);
		edit_duty_man = (TextView)findViewById(R.id.edit_duty_man);
		edit_device = (TextView)findViewById(R.id.edit_device);
		edit_break = (TextView)findViewById(R.id.edit_break);
		edit_unit = (TextView)findViewById(R.id.edit_unit);
		edit_jiebao_date = (TextView)findViewById(R.id.edit_jiebao_date);
		edit_confirm_break = (TextView)findViewById(R.id.edit_confirm_break);
		edit_suggestion = (TextView)findViewById(R.id.edit_suggestion);
		edit_repair_man = (TextView)findViewById(R.id.edit_repair_man);
		edit_predict_date = (TextView)findViewById(R.id.edit_predict_date);
		edit_repair_result = (TextView)findViewById(R.id.edit_repair_result);
		edit_finish = (TextView)findViewById(R.id.edit_finish);
		
		edit_deparment.setEnabled(false);
		edit_shangbao_date.setEnabled(false);
		edit_duty_man.setEnabled(false);
		edit_device.setEnabled(false);
		edit_break.setEnabled(false);
		edit_unit.setEnabled(false);
		edit_jiebao_date.setEnabled(false);
		edit_confirm_break.setEnabled(false);
		edit_suggestion.setEnabled(false);
		edit_repair_man.setEnabled(false);
		edit_predict_date.setEnabled(false);
		edit_repair_result.setEnabled(false);
		edit_finish.setEnabled(false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.btn_refresh:
			//提交维修结果
			
			break;
		default:
			break;
		}
	}
}
